package com.aaa.controller;

import com.aaa.alibaba.ocr_idcard;
import com.aaa.entity.Emp;
import com.aaa.entity.Role;
import com.aaa.service.EmpService;
import com.aaa.service.RoleService;
import com.aaa.util.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("emp")
public class EmpController {
    @Resource
    EmpService empService;
    @Resource
    RoleService roleService;

    @RequestMapping("selectEmp")
    @ResponseBody
    public PageInfo select(Integer pageNum, Integer pageSize, String ename) {
        if(ename!=null){
            //模糊查询员工名根据
            PageHelper.startPage(pageNum,pageSize);
            List<Emp> list=empService.selectEmp(ename);
            PageInfo pageInfo=new PageInfo(list);
            return  pageInfo;
        }else {
            //查询全部
            PageHelper.startPage(pageNum,pageSize);
            List<Emp> list=empService.selectEmpAll();
            PageInfo pageInfo=new PageInfo(list);
            return  pageInfo;
        }

    }
    //批量删除
    @RequestMapping("delEmp")
    @ResponseBody
    public boolean delEmp(String str) {
    String[] eid= str.split(",");
    List<String> eidList=new ArrayList<>();
    for (int i=0;i<eid.length;i++){
        eidList.add(eid[i]);
    }
        boolean b=false;
    QueryWrapper queryWrapper=new QueryWrapper();
    queryWrapper.eq("state",1);
         queryWrapper.in("eid", eidList);
      List<Emp> list=empService.list(queryWrapper);
      if(list.size()==0){
          for (int i=0;i<eid.length;i++) {
              b = empService.removeById(eid[i]);
          }
      }
    return b;
    }
    //员工离职
    @RequestMapping("empLeave")
    @ResponseBody
    public boolean empLeave(Integer eid) {
        Emp emp=new Emp();
        emp.setEid(eid);
        emp.setState(2);
        emp.setLeavedate(new Date());

        return   empService.updateById(emp);
    }
    @RequestMapping("updEmp")
    @ResponseBody
    public boolean updEmp(Emp emp,Integer roleid) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("roleid",roleid);
        List<Emp> list = empService.list(queryWrapper);
        boolean b = empService.updateById(emp);

        return true;
    }
    //头像修改
    @RequestMapping("upload")
    @ResponseBody
    public boolean upload(@RequestParam("file") MultipartFile file, String eusername, HttpServletRequest request) {
    String  photo= FileUtil.upload(file,"",request);
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("eusername",eusername);
        updateWrapper.set("photo",photo);
        return empService.update(updateWrapper);
    }
    //员工添加
    @RequestMapping("empAdd")
    @ResponseBody
    public boolean empAdd(Emp emp) {
        //密码编译
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String epass = bCryptPasswordEncoder.encode(emp.getEpass());
        emp.setEpass(epass);
        emp.setHiredate(new Date());
        emp.setPhoto("yyx.gif");
        emp.setRoleid(1);
        emp.setState(1);
        emp.setAuthstate(2);
       boolean result=empService.save(emp);
        return result;
    }
    //员工认证·
    @RequestMapping("empAuth")
    @ResponseBody
    public Map<String,Object> empAuth(@RequestParam("file") MultipartFile file, String eusername, HttpServletRequest request, String zfm) {
        String  idcardImg= FileUtil.upload(file,"",request);
        String filePath = "D:\\AAA课程\\S3\\前后端分离项目1\\vue-manage-system-4.2.0\\src\\assets\\img\\";//文件真实路径
        //把阿里云提供的接口封装成一个方法的身份证识别，只需要传递一个图片路径，和一个正反面
        Map<String, Object> map = ocr_idcard.infoIdcard(filePath+idcardImg, zfm);
        if(map.containsKey("issue")){
            Emp emp=new Emp();
            emp.setAuthstate(1);
            UpdateWrapper updateWrapper=new UpdateWrapper();
            System.out.println(eusername);
            updateWrapper.eq("eusername",eusername);
            empService.update(emp,updateWrapper);
            return map;
        }else if(map.containsKey("address")){
            return map;
        }
        else {
            //识别失败不管正反面都改为认证失败
            Emp emp=new Emp();
            emp.setAuthstate(2);
            UpdateWrapper updateWrapper=new UpdateWrapper();
            updateWrapper.eq("eusername",eusername);
            empService.update(emp,updateWrapper);
            return null;
        }
    }
    //修改密码
    @RequestMapping("empUpdEpass")
    @ResponseBody
    public Object empUpdEpass(String newepass,String eusername,String epass) {
        //编译密码
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String encodeepass = bCryptPasswordEncoder.encode(newepass);
            UpdateWrapper updateWrapper=new UpdateWrapper();
            updateWrapper.eq("eusername",eusername);
            updateWrapper.set("epass",encodeepass);
            Emp emp=new Emp();
            emp.setEpass(encodeepass);
            empService.update(emp,updateWrapper);
            return true;
    }
    //查询用户认证状态
    @RequestMapping("selectEmpByEusername")
    @ResponseBody
    public boolean selectEmpByEusername( String eusername) {
        QueryWrapper queryWrapper=new QueryWrapper();
         queryWrapper.eq("eusername", eusername);
         List<Emp> list=empService.list(queryWrapper);
         if (list.get(0).getAuthstate()==0){//等于零是没有认证可以继续认证
             return true;
         }else {//不等零已经认证成功
             return false;
         }

    }
    //用于给下拉框赋值
    @RequestMapping("selectRole")
    @ResponseBody
    public List<Role> selectRole(){
        List<Role> list = roleService.list();
        return list;
    }
}
